package us.mooresr.webtroopmanager.client.ui;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import us.mooresr.webtroopmanager.client.place.RoleListPlace;
import us.mooresr.webtroopmanager.shared.Privilege;
import us.mooresr.webtroopmanager.shared.Role;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class EditRoleViewImpl extends Composite implements EditRoleView {
	private Presenter presenter;
	private Long roleId;
	private ArrayList<PrivilegeCheckBoxControl> privilegeCheckBoxControl;

	private static EditRoleViewImplUiBinder uiBinder = GWT
			.create(EditRoleViewImplUiBinder.class);

	interface EditRoleViewImplUiBinder extends
			UiBinder<Widget, EditRoleViewImpl> {
	}
	
	@UiField
	TextBox roleNameTextBox;
	
	@UiField
	ScrollPanel panel;
	
	@UiField
	Button okButton;
	
	@UiField
	Button cancelButton;
	
	@UiHandler("cancelButton")
	void onCancelButton(ClickEvent e) {
		presenter.goTo(new RoleListPlace());
	}
	
	@UiHandler("okButton")
	void onOkButton(ClickEvent e) {
		Set<Privilege> privileges = new HashSet<Privilege>();
		
		for (PrivilegeCheckBoxControl c : privilegeCheckBoxControl) {
			if (c.getC().getValue() == true) {
				privileges.add(c.getP());
			}		
		}
		
		Role newRole = new Role(roleId, roleNameTextBox.getText(), privileges);
		
		presenter.addRole(newRole);
		presenter.goTo(new RoleListPlace());
		
	}

	public EditRoleViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
		panel.setHeight("400px");
		
	
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		
	}

	@Override
	public void setData(Role role) {
		if (role == null) {
			roleId = null;
			roleNameTextBox.setValue("");		
		} else {
			roleId = role.getId();
			roleNameTextBox.setValue(role.getRoleName());
		}
		
		privilegeCheckBoxControl = new ArrayList<PrivilegeCheckBoxControl>();
		
		Set<Privilege> grantedPrivileges;
		if (roleId == null) {
			grantedPrivileges = new HashSet<Privilege>();
		} else {
			grantedPrivileges = role.getPrivileges();
		}
		
		Grid g = new Grid(Privilege.values().length, 1);
		
		// Iterate over all possible privileges.  For each one we create a checkbox,
		// then check grantedPrivileges to decide if that checkbox should be checked or not.
		// Then we create a control for that privilege/checkbox combination and add it to the grid
		
		int i=0;
		for (Privilege p : Privilege.values()) {
			CheckBox c = new CheckBox(p.toString());
			if (grantedPrivileges.contains(p)) {
				c.setValue(true);
			} else {
				c.setValue(false);
			}
			g.setWidget(i, 0, c);
			privilegeCheckBoxControl.add(new PrivilegeCheckBoxControl(p, c));
			++i;
		}
		
		panel.setWidget(g);
		
	}
	
	/**
	 * A control which represents a privilege and has a checkbox that can be turned on or off
	 * 
	 * @author christopher_moore
	 *
	 */
	private class PrivilegeCheckBoxControl {
		private Privilege p;
		private CheckBox c;
		
		public PrivilegeCheckBoxControl(Privilege p, CheckBox c) {
			this.p = p;
			this.c = c;
		}
		
		public Privilege getP() {
			return p;
		}
		public void setP(Privilege p) {
			this.p = p;
		}
		public CheckBox getC() {
			return c;
		}
		public void setC(CheckBox c) {
			this.c = c;
		}
		
		
		
		
	}



}
